package com.fuluwa.domain.file.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fuluwa.domain.file.model.FileInfo;
import com.fuluwa.infra.exception.PersistenceException;
import com.fuluwa.infra.lucene.LuceneService;
import com.fuluwa.infra.web.Result;

public class FileInfoLuceneServiceImpl implements FileInfoLuceneService {

	@Autowired
	LuceneService luceneService;
	
	@Override
	public Result indexAddOrUpdadate(Long fileId) throws PersistenceException, IOException {
		FileInfo fileInfo = new FileInfo(fileId);
		fileInfo = fileInfo.load();
		
		
		Map<String,Object> fields=new HashMap<String, Object>();
		fields.put("file_name",fileInfo.getName() );
//		fields.put("file_catagory",fileInfo.getType().toString() );
		fields.put("file_content", FileUtils.readFileToString(fileInfo.getPhiscalPathFile()) );
		
		//TODO file_abstract,file_keyword
		
		return luceneService.indexAddOrUpdadate(fileInfo.getClass().getCanonicalName()+"__"+fileId, fields);
	}

}
